package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.dao.UserDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author zhangmeng
 * @description 用户服务实现类
 * @date 2019/9/6
 **/
@Slf4j
@DubboService
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public User findByUsername(String username) {
        //先查基本信息
        User user = userDao.selectByUsername(username);
        if (null == user) {
            //用户不存在
            return null;
        }
        //根据用户ID查角色信息
        List<Role> roles = roleDao.selectByUserId(user.getId());
        for (Role role : roles) {
            //根据角色ID查权限信息
            List<Permission> permissions = permissionDao.selectByRoleId(role.getId());
            role.getPermissions().addAll(permissions);
        }
        user.getRoles().addAll(roles);
        return user;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Role> page = userDao.selectByQueryString(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page);
    }

    @Override
    public User findById(Integer id) {
        return userDao.findByUserId(id);
    }

    @Override
    @Transactional
    public void add(Integer[] roleIds, User user) {
        userDao.addrole(user);
        userDao.addRoleIds(roleIds,user.getId());
    }

    @Override
    @Transactional
    public void edit(Integer[] roleIds, User user) {
        userDao.edit(user);
        userDao.deleteByUserid(user.getId());
        userDao.addRoleIds(roleIds,user.getId());
    }

    @Override
    @Transactional
    public void del(Integer id) {
        try {
            userDao.deleteByRoleid(id);
            userDao.del(id);
        } catch (Exception e) {
            throw new BusinessRuntimeException("数据关联无法删除");
        }
    }

    @Override
    public List<Menu> findByName(String username) {
        //先查基本信息
        User user = userDao.selectByUsername(username);
        //创建一个容器
          List<Menu> menus = new ArrayList<>();
        if (null == user) {
            //用户不存在
            return null;
        }
        //根据用户ID查角色信息
        List<Role> roles = roleDao.selectByUserId(user.getId());
        for (Role role : roles) {
            //根据角色ID查菜单信息    有一个问题有多个角色的时候 如何判断最大的菜单显示
            //没有菜单dao  先用role角色的dao 写下
            List<Menu> menuList = roleDao.selectByMenuId(role.getId());
            for (Menu menu : menuList) {
                if (null == menu.getParentMenuId()){
                    //把每一个菜单传进去
                    List<Menu>childrens  = roleDao.selectByparentMenuId(menu.getId());
                    for (Menu children : childrens) {

                        menu.getChildren().add(children);
                    }
                    menus.add(menu);
                }
            }
        }
        return menus;
    }
}
